/**
 * Created by Administrator on 15-7-1.
 */
var Dialog = RichBase.extend({
	statics : {
		oSrc : "http://www.12301.cc/wx_public/images/nopic.jpg",
		loadingSrc : "http://www.12301.cc/images/nopic_loading.png",
		errorSrc : "http://www.12301.cc/images/nopic_error.png",
		imgUpload : {
			fileType : ["jpg","png","gif","jpeg","bmp"]
		}
	},
	tarInp : null,
	tarImg : null,
	EVENTS : {
		"click" : {
			"#closeImgDialogBtn" : "onClose",
			"#cancelImgDialogBtn" : "onClose",
			"#submitImgUploadBtn" : "onCertainBtnClick",
			"#imgDialog-tabNavCon .dialog-tabNav" : "onDialogTabNavClick"
		},
		"change" : {
			"#imgUpload_inpFile" : "onFileInpChange"
		}
	},
	onClose : function(that,e){
		that.container.hide();
	},
	onCertainBtnClick : function(that,e){
		var tarInp = that.tarInp;
		var tarImg = that.tarImg;
		var tarTab = $("#imgDialog-tabNavCon").children(".active");
		var oSrc = that.statics.oSrc;
		var loadingSrc = that.statics.loadingSrc;
		var errorSrc = that.statics.errorSrc;
		var img = new Image();
		var imgSrc = "";
		if(tarTab.hasClass("imgnamespance")){ //网络图片
			imgSrc = $("#imgUpload_space_inpFile").val();
			if(!imgSrc) return false;
			tarInp.val(imgSrc);
			tarImg.attr("src",loadingSrc);
			if(img.complete) tarImg.attr("src",imgSrc);
			img.onload = function(){
				tarImg.attr("src",imgSrc);
			}
			img.onerror = function(){
				tarImg.attr("src",errorSrc);
			}
			img.src = imgSrc;
			that.onClose(that,e);
		}else{ //自已上传
			var fileInp = $("#imgUpload_inpFile");
			if(!$("#imgUpload_fileVal").val()) return false;
			$("#imgUpload_form").submit();
			that.onClose(that,e);
		}
	},
	onFileInpChange : function(that,e){
		var fileInp = $(e.currentTarget);
		var fileVal = e.currentTarget.value;
		var fileType = fileVal.substring(fileVal.indexOf(".")+1);
		var _FILETYPE = that.statics.imgUpload.fileType;
		var allow = false;
		for(var i in _FILETYPE){
			if(fileType==_FILETYPE[i]){
				allow = true;
				break;
			}
		}
		if(!allow){
			alert("文件格式不符合");
			fileInp.val("");
			$("#imgUpload_fileVal").val("");
		}else{
			$("#imgUpload_fileVal").val(fileVal);
		}
	},
	onImguploadComplete : function(data){
		var status = data.status;
		var imgSrc = data.key;
		var tarInp = this.tarInp;
		var tarImg = this.tarImg;
		var loadingSrc = this.statics.loadingSrc;
		var errorSrc = this.statics.errorSrc;
		if(status=="fail"){
			alert("上传失败，请稍后重试");
		}else{
			var img = new Image();
			tarInp.val(imgSrc);
			tarImg.attr("src",loadingSrc);
			if(img.complete) tarImg.attr("src",imgSrc);
			img.onload = function(){
				tarImg.attr("src",imgSrc);
			}
			img.onerror = function(){
				tarImg.attr("src",errorSrc);
			}
			img.src = imgSrc;
		}
	},
	onDialogTabNavClick : function(that,e){
		var tarBtn = $(e.currentTarget);
		if(tarBtn.hasClass("active")) return false;
		tarBtn.addClass("active").siblings().removeClass("active");
		$("#imgDialog-tabPanCon").children().removeClass("active").eq(tarBtn.index()).addClass("active");
	},
	open : function(tarInp,tarImg){
		this.container.show();
		this.tarInp = tarInp;
		this.tarImg = tarImg;
	}
});